//
//  XJShouCangViewController.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/5/14.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  我的收藏
//  使用加载子视图控制器的方式有个致命的问题:子控制器挂了。不停的被销毁。或许思路本就不对。

#import "XJShouCangViewController.h"

//Controller
#import "XJShouCangGoodsViewController.h"
#import "XJShouCangTopicViewController.h"

@interface XJShouCangViewController ()<UIScrollViewDelegate>
@property(nonatomic, strong) UIView *titleView;
@property(nonatomic, strong) UIView *yellowIndicateView; // 黄色线条指示器
/**当前选中的标题按钮*/
@property(nonatomic, strong) UIButton *selectedBtn;
/**装子VC的view*/
@property(nonatomic, strong) UIScrollView *scrollView;

@property(nonatomic, strong) UIButton *rightEditButton;

//1.需要记录编辑按钮的状态。
//2.需要将编辑按钮的状态传递到“收藏的商品控制器” 或 “收藏的帖子控制器”
/**收藏的商品*/
@property (nonatomic, strong) XJShouCangGoodsViewController *goodsVC;
/**收藏的帖子*/
@property (nonatomic, strong) XJShouCangTopicViewController *topicsVC;

@end

@implementation XJShouCangViewController
#pragma mark - 一 lazy

- (UIButton *)rightEditButton {
    if (!_rightEditButton) {
        _rightEditButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [_rightEditButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [_rightEditButton setTitleColor:[UIColor blackColor] forState:(UIControlStateSelected)];
        [_rightEditButton setTitle:@"编辑" forState:(UIControlStateNormal)];
        [_rightEditButton setTitle:@"完成" forState:(UIControlStateSelected)];
        
        //设置frame
        _rightEditButton.frame = CGRectMake(0, 0, 40, 30);
        
        [_rightEditButton addTarget:self action:@selector(rightEditButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _rightEditButton;
}


#pragma mark - 二 生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupNav];
    
    [self setupChildViewControllers];
    
    [self setupScrollView];
    
    [self addChildVcViews];
    
    [self setupTitleView];
}

#pragma mark - 三 UI
- (void)setupNav {
    self.navigationItem.title = @"我的收藏";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightEditButton];
}


- (void)rightEditButtonAction:(UIButton *)btn {
    btn.selected = !btn.selected;
    //需要知道当前显示的是哪个控制器。怎么判断。代码如下 index=0表示当前显示的是第一个控制器(收藏的商品控制器)  =1表示当前显示的是第二个控制器(收藏的帖子控制器)
    NSInteger index = self.scrollView.contentOffset.x / self.scrollView.rb_width;
    
    if (index == 0) { //收藏的商品控制器
        //        XJLog(@"编辑收藏的商品控制器:%ld",index);
//        XJLog(@"=====:%@",btn.selected ? @"YES":@"NO");
        self.goodsVC.scgoodsvcIsEditing = btn.selected;
    }else{ //收藏的帖子控制器
        //        XJLog(@"编辑收藏的帖子控制器:%ld",index);
//        XJLog(@"=====:%@",btn.selected ? @"YES":@"NO");
        self.topicsVC.sctopicvcIsEditing = btn.selected;
    }
    
    //2.将编辑控制器(self)的编辑状态传递给 子控制器(XJShouCangTopicViewController, XJShouCangGoodsViewController) 并且同时只能有一个控制器处于编辑状态。
    //3.切换子控制器的时候要对应的切换右上角编辑按钮的状态。单独提供一个方法调用。
}

- (void)setupTitleView {
    NSArray *titleArr = @[@"收藏的商品",@"收藏的帖子"];
    CGFloat btn_h = 35;
    CGFloat btn_w = KWidth / titleArr.count;

    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KWidth, btn_h)];
    
    [self.view addSubview:titleView];
    for (int i = 0; i<titleArr.count; i++) {
        UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [titleView addSubview:btn];
        btn.frame = CGRectMake(btn_w * i, 0, btn_w, btn_h);
        btn.tag = i;
        [btn setTitle:titleArr[i] forState:(UIControlStateNormal)];
        [btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        [btn addTarget:self action:@selector(titleClickAction:) forControlEvents:(UIControlEventTouchUpInside)]; //点击事件
    }
    
    // 赋值
    self.titleView = titleView;

    // 获取第一个 button
    UIButton *firstBtn = titleView.subviews.firstObject;
    
    // 底部文字指示器
    UIView *indicateView = [UIView new];
    indicateView.backgroundColor = XJCOLOR_RGB(253, 212, 49);
    indicateView.rb_height = 3;
    indicateView.rb_y = titleView.rb_height - indicateView.rb_height;
    
    // 提前计算好文字长度
    [firstBtn.titleLabel sizeToFit];
    indicateView.rb_width = firstBtn.titleLabel.rb_width;
    indicateView.rb_center_x = firstBtn.rb_center_x;
    
    [titleView addSubview:indicateView];
    
    // 赋值
    self.yellowIndicateView = indicateView;
}

#pragma mark - 三- 1 点击按钮
- (void)titleClickAction:(UIButton *)btn {
    self.selectedBtn.selected = NO;
    btn.selected = YES;
    self.selectedBtn = btn;
    
    // 黄色线条动画
    [UIView animateWithDuration:0.25 animations:^{
        self.yellowIndicateView.rb_width = btn.titleLabel.rb_width;
        self.yellowIndicateView.rb_center_x = btn.rb_center_x;
    }];
    
    //让scrollView滚到对应的位置
    CGPoint offset = self.scrollView.contentOffset;
    offset.x = btn.tag * _scrollView.rb_width;
    [self.scrollView setContentOffset:offset animated:YES];
}

// 添加子视图控制器
- (void)setupChildViewControllers {
    XJShouCangGoodsViewController *goodsVC = [[XJShouCangGoodsViewController alloc] init];
    self.goodsVC = goodsVC;
    [self addChildViewController:goodsVC];
    
    XJShouCangTopicViewController *topicVC = [[XJShouCangTopicViewController alloc] init];
    self.topicsVC = topicVC;
    [self addChildViewController:topicVC];
}

- (void)setupScrollView {
    // 作用:不允许自动调整scrollView的内边距,让scrollView全屏
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
//    scrollView.backgroundColor = [UIColor yellowColor];
    scrollView.delegate = self;
    scrollView.pagingEnabled = YES;
    //设置scrollView的高度。
    scrollView.frame = CGRectMake(0, 40, KWidth, KHeight - 40 - kStatusBarAndNavigationBarHeight);
    NSInteger cnt = self.childViewControllers.count;
    scrollView.contentSize = CGSizeMake(KWidth * cnt, 0);
    
    self.scrollView = scrollView;
    
    // 别忘了.你要把scrollView添加到self.view上。
    // 还有一个问题就是frame的位置、titleView被盖住了。
    [self.view addSubview:scrollView];
}

// 添加子控制器view添加到scrollView上
- (void)addChildVcViews {
    
    //这要切换控制这个方法就会被调用。在这里切换右上角编辑按钮的状态。
    self.rightEditButton.selected = NO;
    
    // 子控制器indenx
    NSInteger index = self.scrollView.contentOffset.x / self.scrollView.bounds.size.width;

    XJLog(@"index:%ld",index);
    self.topicsVC.sctopicvcIsEditing = NO;
    self.goodsVC.scgoodsvcIsEditing = NO;
    UIViewController *childVC = self.childViewControllers[index];

    // 优化
//    if ([childVC isViewLoaded]) { return;} //这个是有问题的.❌ 今天晚上写笔记。
//        if (childVC.view.superview) return; // 如果supView有值，说明已经被添加到superView上了,就不重复添加 ✅
        if (childVC.view.window) return; // 如果window有值，说明已经被加到window上 ✅
    
    childVC.view.frame = self.scrollView.bounds;
    
    [self.scrollView addSubview:childVC.view];
}


#pragma mark - 四 UIScrollViewDelegate
/**
 必须调用了这个方法 才会走
 setContentOffset: animated:
 
 作用:点击顶部标题按钮 切换对应的view
 */
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    [self addChildVcViews];
}

/**
 目标:人为左右拖动scrollView 切换对应的标题。
 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger index = scrollView.contentOffset.x / scrollView.rb_width;
    
    // 拿到对应下标的btn
    UIButton *btn = self.titleView.subviews[index];
    
    [self titleClickAction:btn];
    
    [self addChildVcViews];
}


#pragma mark - 五 处理内部业务逻辑
//当切换控制器的时候切换 右上角 编辑按钮的状态。


@end
